package com.hashcoding.eyes.module.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期时间工具类<br>
 *
 * @author markmjw
 * @date 2014-08-20
 */
public final class DateUtil {
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    private DateUtil() {
    }

    public static String formatDateTime(String dateString, String pattern) {
        Date date = parse(dateString, DEFAULT_DATETIME_FORMAT);
        return format(date, pattern);
    }

    public static String formatDate(String dateString, String pattern) {
        Date date = parse(dateString, DEFAULT_DATE_FORMAT);
        return format(date, pattern);
    }

    public static String formatTime(String dateString, String pattern) {
        Date date = parse(dateString, DEFAULT_TIME_FORMAT);
        return format(date, pattern);
    }

    public static String join(String str1, String str2, String divider) {
        String str = "";
        if (!TextUtils.isEmpty(str1)) {
            str = str1;
        }

        if (!TextUtils.isEmpty(str2)) {
            if (TextUtils.isEmpty(str)) {
                str = str2;
            } else {
                str += divider;
                str += str2;
            }
        }

        return str;
    }

    public static Date getNow() {
        return Calendar.getInstance().getTime();
    }

    public static String getDate() {
        return getDateTime(DEFAULT_DATE_FORMAT);
    }

    public static String getDateTime() {
        return getDateTime(DEFAULT_DATETIME_FORMAT);
    }

    public static String getDateTime(String pattern) {
        Date datetime = Calendar.getInstance().getTime();
        return getDateTime(datetime, pattern);
    }

    public static String getDateTime(Date date, String pattern) {
        if (null == pattern || "".equals(pattern)) {
            pattern = DEFAULT_DATETIME_FORMAT;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        return dateFormat.format(date);
    }

    public static int getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getCurrentMonth() {
        // 用get得到的月份数比实际的小1，需要加上
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    public static int getCurrentDay() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    public static Date parse(String dateStr, String pattern) {
        Date date = null;
        if (!TextUtils.isEmpty(dateStr)) {
            if (null == pattern || "".equals(pattern)) {
                pattern = DEFAULT_DATE_FORMAT;
            }
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
                date = dateFormat.parse(dateStr);
            } catch (ParseException e) {
                // ignore
            }
        }
        return date;
    }

    public static String format(Date date, String pattern) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
            return dateFormat.format(date);
        }
    }

    public static String format(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT, Locale
                    .CHINA);
            return dateFormat.format(date);
        }
    }

    public static Date getMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    public static String getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (day < 10) {
            return "0" + day;
        } else {
            return day + "";
        }
    }

    public static String getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);

        return year + "";
    }

    public static Date addDays(Date date, int days) {
        return add(date, days, Calendar.DATE);
    }

    private static Date add(Date date, int amount, int field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    public static long getTimeMorning(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis() / 1000;
    }

    public static long getTimeNight(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis() / 1000;
    }
}
